//修正动画显示层级

let SWDA_BattleAnimation = {};

//新增敌我之间的动画层（仅战斗界面、非我方角色使用）
SWDA_BattleAnimation.Spriteset_Battle_createLowerLayer = Spriteset_Battle.prototype.createLowerLayer;
Spriteset_Battle.prototype.createLowerLayer = function () {
    SWDA_BattleAnimation.Spriteset_Battle_createLowerLayer.call(this);
    this.createMidAnimations(); 
};

// 新增中间动画层
Spriteset_Battle.prototype.createMidAnimations = function () {
    this._midAnimationSprites = [];
    this._midAnimationSprites = new Sprite_Base();
    this._midAnimationSprites._isMidAni = true;
    this._battleField.addChild(this._midAnimationSprites);

};

// 补充Sprite类型的标记
SWDA_BattleAnimation.Sprite_Actor_initMembers = Sprite_Actor.prototype.initMembers;
Sprite_Actor.prototype.initMembers = function () {
    SWDA_BattleAnimation.Sprite_Actor_initMembers.call(this);
    this._isActorSprite = true;
}

SWDA_BattleAnimation.Sprite_Enemy_initMembers = Sprite_Enemy.prototype.initMembers;
Sprite_Enemy.prototype.initMembers = function () {
    SWDA_BattleAnimation.Sprite_Enemy_initMembers.call(this);
    this._isEnemySprite = true;
}


SWDA_BattleAnimation.Sprite_Animation_initMembers = Sprite_Animation.prototype.initMembers;
Sprite_Animation.prototype.initMembers = function () {
    SWDA_BattleAnimation.Sprite_Animation_initMembers.call(this);
    this._isAniSprite = true;
}

SWDA_BattleAnimation.Sprite_Pet_initialize = Sprite_Pet.prototype.initialize;
Sprite_Pet.prototype.initialize = function(){
    SWDA_BattleAnimation.Sprite_Pet_initialize.call(this);
    this._isPetSprite = true;
    this._isActorSprite = false;    //宠物是基于Actor的，需将此标记置false
};

// 战场Sprite层级排序（背景除外）
// 从底到顶：敌人、中间动画、我方、宠物、动画顶层（已包含所有情况，似乎一旦有缺失就会乱掉）
Spriteset_Battle.prototype.battleFieldDepthCompare = function (a, b) {
    if (a._isAniSprite) { return 1; }
    if (b._isAniSprite) { return -1; }
    if (a._isMidAni && b._isActorSprite) { return -1; }
    if (a._isMidAni && b._isEnemySprite) { return 1; }
    if (a._isMidAni && b._isPetSprite) { return -1; }
    if (a._isActorSprite && b._isMidAni) { return 1; }
    if (a._isPetSprite && b._isMidAni) { return 1; }
    if (a._isEnemySprite && b._isMidAni) { return -1; }
    if (a._isPetSprite && b._isEnemySprite) { return 1; }
    if (a._isPetSprite && b._isActorSprite) { return 1; }
    if (a._isEnemySprite && b._isPetSprite) { return -1; }
    if (a._isActorSprite && b._isPetSprite) { return -1; }
    if (a._isActorSprite && b._isEnemySprite) { return 1; }
    if (a._isEnemySprite && b._isActorSprite) { return -1; }
    if (a._isPetSprite && b._isPetSprite) {
        if (a.z < b.z) return -1;
        if (a.z > b.z) return 1;
        if (a.y < b.y) return -1;
        if (a.y >= b.y) return 1;
        if (a.x < b.x) return -1;
        if (a.x > b.x) return 1;
        if (a.spriteId > b.spriteId) return 1;
    }
    if (a._isActorSprite && b._isActorSprite) {
        if (a.z < b.z) return -1;
        if (a.z > b.z) return 1;
        if (a.y < b.y) return -1;
        if (a.y > b.y) return 1;
        if (a.x < b.x) return -1;
        if (a.x > b.x) return 1;
        if (a.spriteId > b.spriteId) return 1;
    }
    if (a._isEnemySprite && b._isEnemySprite) {
        if (a.z < b.z) return -1;
        if (a.z > b.z) return 1;
        if ( a.y + a.scale.y * a._enemy.spriteHeight() * (1 - a.anchor.y) < b.y + b.scale.y * b._enemy.spriteHeight() * (1 - b.anchor.y)) return -1;
        if ( a.y + a.scale.y * a._enemy.spriteHeight() * (1 - a.anchor.y) > b.y + b.scale.y * b._enemy.spriteHeight() * (1 - b.anchor.y)) return 1;
        if (a.x < b.x) return -1;
        if (a.x > b.x) return 1;
        if (a.spriteId > b.spriteId) return 1;
    }
    return 0;
};


// 我方角色动画分上下两种情况
SWDA_BattleAnimation.Sprite_Actor_startAnimation = Sprite_Actor.prototype.startAnimation;
Sprite_Actor.prototype.startAnimation = function (animation, mirror, delay) {
    mirror = false;
    if (animation.name.contains("BLW")) {
        // 如果是我方角色，若动画为below模式（动画数据库中标记Below），则在中层，否则默认顶层
        var sprite = new Sprite_Animation();
        sprite.setup(this._effectTarget, animation, mirror, delay);
        this.parent.parent.parent._midAnimationSprites.addChild(sprite);
        this.parent.parent.parent._midAnimationSprites._animationSprites.push(sprite);
    } else {
        SWDA_BattleAnimation.Sprite_Actor_startAnimation.call(this, animation, mirror, delay);
    }
};

//敌方动画只能显示在敌我之间的中间层
SWDA_BattleAnimation.Sprite_Enemy_startAnimation = Sprite_Enemy.prototype.startAnimation;
Sprite_Enemy.prototype.startAnimation = function (animation, mirror, delay) {
    if ($gameParty.inBattle()) {
        // 战斗中，敌人角色动画显示在中层，否则默认顶层
        var sprite = new Sprite_Animation();
        sprite.setup(this._effectTarget, animation, mirror, delay);
        this.parent.parent.parent._midAnimationSprites.addChild(sprite);
        this.parent.parent.parent._midAnimationSprites._animationSprites.push(sprite);
    } else {
        SWDA_BattleAnimation.Sprite_Enemy_startAnimation.call(this, animation, mirror, delay);
    }
};

// 
Spriteset_Battle.prototype.isAnimationPlaying = function() {
    var allAnimationSprites = this.battlerSprites().concat(this._midAnimationSprites);  //补充中间层动画
    return allAnimationSprites.some(function(sprite) {
        return sprite.isAnimationPlaying();
    });
};

//修正动画坐标
SWDA_BattleAnimation.Sprite_Animation_updatePosition = Sprite_Animation.prototype.updatePosition;
Sprite_Animation.prototype.updatePosition = function () {
    if ($gameParty.inBattle()) {
        // 画面
        if (this._animation.position === 3) {
            this.x = Graphics.boxWidth / 2;
            this.y = Graphics.boxHeight / 2;
            if (this.isOnActor()){
                this.y += 150;
            } 
        } else {
            var parent = this._target.parent;
            var grandparent = parent ? parent.parent : null;
            this.x = this._target.x;
            this.y = this.isOnEnemy() ? this._target.realY() : this._target.y;  // 修正因人为设定敌人坐标产生的动画偏移问题
            // 脚底
            if (this.parent === grandparent) {
                this.x += parent.x;
                this.y += parent.y;
            }
            // 头顶
            if (this._animation.position === 0) {
                this.y -= this._target.height;
            // 中心
            } else if (this._animation.position === 1) {
                this.y -= this._target.height / 2;
            }
        }
        if (this._animation.name.contains("BLW")) {
            if(!!parent){
                this.x += parent.x;
                this.y += parent.y;
            }
            
        }
    } else {
        SWDA_BattleAnimation.Sprite_Animation_updatePosition.call(this);
    }
};


// 修正我方动画尺寸
SWDA_BattleAnimation.Sprite_Animation_updateCellSprite = Sprite_Animation.prototype.updateCellSprite;
Sprite_Animation.prototype.updateCellSprite = function(sprite, cell) {
    SWDA_BattleAnimation.Sprite_Animation_updateCellSprite.call(this, sprite, cell);
    var pattern = cell[0];
    // 新增部分：当是我方动画时，放大一定比例
    if (pattern >= 0) {
        if (this.isOnActor()){
            const zoomRate = 1.5;
            sprite.x *= zoomRate;
            sprite.y *= zoomRate;
            sprite.scale.x *= zoomRate;
            sprite.scale.y *= zoomRate;
        }
    }
};

Sprite_Animation.prototype.isOnActor = function(){
    if ($gameParty.inBattle()) 
        if(this._target.parent)
            if(this._target.parent._battler)
                return this._target.parent._battler.isActor();
    return false;
}
Sprite_Animation.prototype.isOnEnemy = function(){
    if ($gameParty.inBattle()) {
        if(!!this._target)
            if(!!this._target._battler)
                if(this._target._battler.isEnemy()){return true;}
        if(this._target.parent)
            if(this._target.parent._battler)
                return this._target.parent._battler.isEnemy();
    }


    return false;
}